﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http.Filters;

namespace AspNetDemo.Services
{
    public class ArgumentExceptionHandlerAttribute : System.Web.Http.Filters.ExceptionFilterAttribute
    {
        public override Task OnExceptionAsync(HttpActionExecutedContext actionExecutedContext, CancellationToken cancellationToken)
        {
            if (actionExecutedContext.Exception is ArgumentException)
            {
                actionExecutedContext.Response = new System.Net.Http.HttpResponseMessage(System.Net.HttpStatusCode.InternalServerError)
                {
                    Content = new StringContent(actionExecutedContext.Exception.Message,Encoding.UTF8)
                };

                return Task.FromResult(0);
            }
            return base.OnExceptionAsync(actionExecutedContext, cancellationToken);
        }
    }
}